package com.aigou.admin.controller;
import com.aigou.admin.annotation.ResponseResult;
import com.aigou.admin.entity.vo.ResponseStatusEnum;
import com.aigou.admin.utils.OssUtil;
import com.aigou.admin.entity.vo.ResponseVo;
import lombok.AllArgsConstructor;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: Hor
 * @Date: 2024/12/6 10:58
 * @Version: 1.0
 */

@RestController
@AllArgsConstructor
@ResponseResult
public class UploadController {

    @RequestMapping("/upload")
    protected ResponseVo upload(MultipartFile file) throws IOException {
        try {
            OssUtil.upload(file.getInputStream(), file.getName());
            String url = OssUtil.getUrl(file.getName());
            return ResponseVo.buildSuccess(url);
        } catch (FileSizeLimitExceededException ex) {
            ex.printStackTrace();
            return ResponseVo.buildFail(ResponseStatusEnum.FAIL,"文件过大");
        }
    }
}
